<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <canvas width="512" height="512"></canvas>
  </body>

  <script>
    const canvas = document.querySelector('canvas')
    const context = canvas.getContext('2d')

    let cx = canvas.width / 2
    let cy = canvas.height / 2

    
    context.fillStyle = 'red'
    context.save()  //暂存状态
    context.beginPath()
    context.translate(-50,-50)
    context.rect(cx , cy , 100, 100)

    context.fill()
    // context.translate(50,50)
    context.restore()  //恢复状态
  </script>

</html>
